﻿using ST.Library.UI.NodeEditor;
using STNode_A2B.DataType;
using STNode_A2B.STNodeControls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STNode_A2B.STNodes
{
    [STNode("A2B")]
    public class STNode_Host : STNode
    {
        private STNodeOption m_op_in_IIC;
        private STNodeOption m_op_out_Tx0;
        private STNodeOption m_op_out_Tx1;
        private STNodeOption m_op_out_Rx0;
        private STNodeOption m_op_out_Rx1;
        private STNodeOption m_op_out_emyty;

        private STNodeControl_Label m_ctrl_label_Adr;
        private STNodeControl_Label m_ctrl_label_Tx0;
        private STNodeControl_Label m_ctrl_label_Tx1;
        private STNodeControl_Label m_ctrl_label_Rx0;
        private STNodeControl_Label m_ctrl_label_Rx1;

        private STNodeControl_TextBox m_ctrl_TextBox_Adr;
        private STNodeControl_TextBox m_ctrl_TextBox_Tx0;
        private STNodeControl_TextBox m_ctrl_TextBox_Tx1;
        private STNodeControl_TextBox m_ctrl_TextBox_Rx0;
        private STNodeControl_TextBox m_ctrl_TextBox_Rx1;

        private int _IIC_Addr = 44;
        [STNodeProperty("IIC Address", "IIC Address")]
        public string Addr
        {
            get { return _IIC_Addr.ToString(); }
            set
            {
                int temp;
                if (int.TryParse(value, out temp))
                {
                    m_ctrl_TextBox_Adr.EditorText = value;
                    _IIC_Addr = int.Parse(value);
                    Invalidate();
                }               
            }
        }

        private int _Rx0_Data;
        [STNodeProperty("Rx0", "Tx0 Data")]
        public string Rx0_Data
        {
            get { return _Rx0_Data.ToString(); }
            set
            {               
                int temp;
                if (int.TryParse(value, out temp))
                {
                    m_ctrl_TextBox_Rx0.EditorText = value;
                    _Rx0_Data = int.Parse(value);
                    
                }
                
            }
        }

        private int _Rx1_Data;
        [STNodeProperty("Rx1", "Tx1 Data")]
        public string Rx1_Data
        {
            get { return _Rx1_Data.ToString(); }
            set
            {
                int temp;              
                if (int.TryParse(value, out temp))
                {
                    m_ctrl_TextBox_Rx1.EditorText = value;
                    _Rx1_Data = int.Parse(value);
                    Invalidate();
                }
                
            }
        }
        private int _Tx0_Data;
        [STNodeProperty("Tx0", "Tx0 Data")]
        public string Tx0_Data
        {
            get { return _Tx0_Data.ToString(); }
            set
            {
                int temp;
                if (int.TryParse(value, out temp))
                {
                    m_ctrl_TextBox_Tx0.EditorText = value;
                    _Tx0_Data = int.Parse(value);
                    Invalidate();
                }
                
            }
        }

        private int _Tx1_Data;
        [STNodeProperty("Tx1", "Tx1 Data")]
        public string Tx1_Data
        {
            get { return _Tx1_Data.ToString(); }
            set
            {
                int temp;
                if (int.TryParse(value, out temp))
                {
                    m_ctrl_TextBox_Tx1.EditorText = value;
                    _Tx1_Data = int.Parse(value);
                    Invalidate();
                }
                
            }
        }

        private string _nodeName = "Host";
        [STNodeProperty("Title", "Title")]
        public string NodeName
        {
            get { return _nodeName; }
            set
            {
                this._nodeName = value;
                this.Title = this._nodeName;
            }
        }

        protected override void OnCreate()
        {
            base.OnCreate();
            this.Title = "Host";
            this.TitleColor = Color.FromArgb(200, Color.Goldenrod);
            this.AutoSize = false;
            this.Size = new Size(180, 130);

            m_op_in_IIC = this.InputOptions.Add("IIC", typeof(IIC), true);

            m_ctrl_label_Adr = new STNodeControl_Label();
            m_ctrl_label_Adr.Size = new Size(95, 20);
            m_ctrl_label_Adr.Location = new Point(20, 0);
            m_ctrl_label_Adr.ForeColor = Color.White;
            m_ctrl_label_Adr.EditorText = "Addr";
            this.Controls.Add(m_ctrl_label_Adr);

            m_ctrl_TextBox_Adr = new STNodeControl_TextBox();
            m_ctrl_TextBox_Adr.IsAddress = true;
            m_ctrl_TextBox_Adr.EditorText = Addr;
            m_ctrl_TextBox_Adr.DisplayRectangle = new Rectangle(100, 0, 50, 20);
            this.Controls.Add(m_ctrl_TextBox_Adr);
            
            m_op_out_emyty = this.OutputOptions.Add("",typeof(NoneType),true);
            m_op_out_emyty.DotSize = 0;
            m_op_out_Tx0 = this.OutputOptions.Add("", typeof(Tx0Rx0), false);
            m_ctrl_label_Tx0 = new STNodeControl_Label();
            m_ctrl_label_Tx0.Size = new Size(80, 20);
            m_ctrl_label_Tx0.Location = new Point(20, 20);
            m_ctrl_label_Tx0.ForeColor = Color.White;
            m_ctrl_label_Tx0.EditorText = "Tx0-ch";
            this.Controls.Add(m_ctrl_label_Tx0);

            m_ctrl_TextBox_Tx0 = new STNodeControl_TextBox();
            m_ctrl_TextBox_Tx0.DisplayRectangle = new Rectangle(100,20,50,20);
            m_ctrl_TextBox_Tx0.EditorText = Tx0_Data;
            this.Controls.Add(m_ctrl_TextBox_Tx0);

            m_op_out_Tx1 = this.OutputOptions.Add("", typeof(Tx1Rx1), false);

            m_ctrl_label_Tx1 = new STNodeControl_Label();
            m_ctrl_label_Tx1.Size = new Size(80, 40);
            m_ctrl_label_Tx1.Location = new Point(20, 30);
            m_ctrl_label_Tx1.ForeColor = Color.White;
            m_ctrl_label_Tx1.EditorText = "Tx1-ch";
            this.Controls.Add(m_ctrl_label_Tx1);

            m_ctrl_TextBox_Tx1 = new STNodeControl_TextBox();
            m_ctrl_TextBox_Tx1.DisplayRectangle = new Rectangle(100, 40, 50, 20);
            m_ctrl_TextBox_Tx1.EditorText = Tx1_Data;
            this.Controls.Add(m_ctrl_TextBox_Tx1);

            m_op_out_Rx0 = this.OutputOptions.Add("", typeof(Rx0Tx0), false);

            m_ctrl_label_Rx0 = new STNodeControl_Label();
            m_ctrl_label_Rx0.Size = new Size(80, 40);
            m_ctrl_label_Rx0.Location = new Point(20, 50);
            m_ctrl_label_Rx0.ForeColor = Color.White;
            m_ctrl_label_Rx0.EditorText = "Rx0-ch";
            this.Controls.Add(m_ctrl_label_Rx0);

            m_ctrl_TextBox_Rx0 = new STNodeControl_TextBox();
            m_ctrl_TextBox_Rx0.DisplayRectangle = new Rectangle(100, 60, 50, 20);
            m_ctrl_TextBox_Rx0.EditorText = Rx0_Data;
            this.Controls.Add(m_ctrl_TextBox_Rx0);

            m_op_out_Rx1 = this.OutputOptions.Add("", typeof(Rx1Tx1), false);

            m_ctrl_label_Rx1 = new STNodeControl_Label();
            m_ctrl_label_Rx1.Size = new Size(80, 40);
            m_ctrl_label_Rx1.Location = new Point(20, 70);
            m_ctrl_label_Rx1.ForeColor = Color.White;
            m_ctrl_label_Rx1.EditorText = "Rx1-ch";
            this.Controls.Add(m_ctrl_label_Rx1);

            m_ctrl_TextBox_Rx1 = new STNodeControl_TextBox();
            m_ctrl_TextBox_Rx1.DisplayRectangle = new Rectangle(100, 80, 50, 20);
            m_ctrl_TextBox_Rx1.EditorText = Rx1_Data;
            this.Controls.Add(m_ctrl_TextBox_Rx1);
        }

        protected override void OnOwnerChanged()
        {
            base.OnOwnerChanged();
            if (this.Owner != null)
            {
                this.Owner.SetTypeColor(typeof(IIC), Color.DarkCyan);
                this.Owner.SetTypeColor(typeof(Tx0Rx0), Color.DarkBlue);
                this.Owner.SetTypeColor(typeof(Tx1Rx1), Color.SkyBlue);
                this.Owner.SetTypeColor(typeof(Rx0Tx0),Color.LightPink);
                this.Owner.SetTypeColor(typeof(Rx1Tx1),Color.DeepPink);
            }
        }

        public bool CheckInputOptionsIsConnect()
        {
            STNodeOption[] sTNodeOptions = this.GetInputOptions();
            bool isAllInputOptionIsConnect = true;
            foreach(STNodeOption inputOption in sTNodeOptions)
            {
                if(inputOption.ConnectionCount == 0)
                {
                    isAllInputOptionIsConnect = false;
                    break;
                }
            }
            return isAllInputOptionIsConnect;
        }

    }
}
